package com.example.p4;

import java.util.ArrayList;
import java.util.Arrays;

import com.playtomic.android.api.*;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.Typeface;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

//HighscoreActivity will list the highscores to the user 
//utilizing a listview: an expandable and scrollable UI piece
public class GameFinish extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_endgame);
        
        Typeface font = Typeface.createFromAsset(getAssets(), "agentorange.ttf");
        
        Button mainMenuButton = (Button) findViewById(R.id.end_game_main_menu_button);
        mainMenuButton.setTextSize(24);
        mainMenuButton.setTextColor(Color.WHITE);
        mainMenuButton.setTypeface(font);
        
        //initialize the buttons' click actions
        mainMenuButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                goToMainMenu();
            }
        });
        
        TextView scoreText = (TextView) findViewById(R.id.end_game_title);
        scoreText.setTextSize(68);
        scoreText.setTextColor(Color.BLACK);
        scoreText.setTypeface(font);
        
        TextView congratsText = (TextView) findViewById(R.id.congrats_text);
        congratsText.setTextSize(36);
        congratsText.setTextColor(Color.BLACK);
        congratsText.setTypeface(font);
        
        ArrayList<String> names = new ArrayList<String>(
                Arrays.asList("Jason","Daniel", "Jerry", "Taylor", "Sam", "Brian", "Stephen", "Katie", "Elizabeth"));
        
        Bundle extras = getIntent().getExtras();
        int score = 0;
        if (extras != null) {
            score = extras.getInt("score");
            scoreText.setText(" "+score+" ");
            leaderboardSave(names.get((int)(Math.random()*(names.size()-1))), score);
        }
        
        congratsText.setText(" Seconds! \n Woohoo! ");
        
        //Test saving a highscore
    } 
    

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    
    protected void goToMainMenu() {
        finish();
    }
    
    //Playtomic highscores
    
    private void leaderboardSave(String name, int newScore) {
        Log.d("Highscores", "Getting leaderboard");
        PlaytomicLeaderboards leaderboards = new PlaytomicLeaderboards();

        // we need to set a listener
        leaderboards.setRequestListener(new PlaytomicRequestListener<PlaytomicScore>() {
            public void onRequestFinished(PlaytomicResponse<PlaytomicScore> playtomicResponse) {
	                if(playtomicResponse.getSuccess()) {
	                    // we call a function for successed cases
	                	requestLeaderBoardSaveFinished();
	                }            
	                else {
	                    // we call a function for failed cases 
	                    requestLeaderBoardSaveFailed(playtomicResponse.getErrorCode(), 
	                    		playtomicResponse.getErrorMessage());
	                }
            	}
        });
        // create a new score
        
        PlaytomicScore score = new PlaytomicScore(name, newScore);

        // save the score
        
        leaderboards.save("Highscores", score, true, true);
    }

    private void requestLeaderBoardSaveFinished() {
        Log.d("Highscores", "Save score success");
    }
        
    private void requestLeaderBoardSaveFailed(int errorCode, String message) {
        Log.d("Highscores", "Leaderboard save failed to save because of errorcode #" + errorCode 
                +  " - Message:" + message);
    }
}

